package org.bjf.modules.shardDemo.controller;

import com.google.common.hash.Funnels;
import lombok.extern.slf4j.Slf4j;
import org.bjf.cache.CacheNameConfig.RedisCacheEnum.Names;
import org.bjf.modules.process.bean.ProcessInfo;
import org.bjf.modules.process.bean.UserAuditInfo;
import org.bjf.modules.process.service.ProcessService;
import org.bjf.modules.process.service.StandardFlowService;
import org.bjf.modules.sys.bean.SysUser;
import org.bjf.modules.user.bean.User;
import org.bjf.modules.user.service.ShardDemoService;
import org.bjf.utils.BloomFilterHelper;
import org.bjf.utils.Http;
import org.bjf.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

@RestController
@Slf4j
@RefreshScope
public class TestController {


  @Resource
  private ShardDemoService shardDemoService;
  @Resource
  private RedisUtil redis;
  @Autowired
  protected RedisTemplate redisTemplate;
  @Resource
  private ProcessService processService;
  @Resource
  private StandardFlowService standardFlowService;

  @RequestMapping("test/user/{id}")
  public User detail(@PathVariable long id) {
    return shardDemoService.get(id);
  }

  @RequestMapping("test/log")
  public Object logger() {
    LocalDateTime now = LocalDateTime.now();
    log.warn("time:" + now);
    boolean lock = redis.getLockLua("gg", 50L);
    log.info("lock ============ " + lock);

    Http.get("http://www.baidu.com");
    return now;
  }

  @Cacheable(value = Names.USER, key = "'live:test:cache:' + #cacheId", sync = true)
  @RequestMapping("test/cache")
  public Object testCache(String cacheId) {
    log.info("from db");
    User user = new User();
    user.setUsername(cacheId);
    return user;
  }

  @RequestMapping("test/up")
  public Object up(@RequestParam("img") MultipartFile file) {

    log.info("xxxxxxxxxxx");

    return "ok";
  }

  @RequestMapping("test/lua")
  public Object lock(String key,String name) {

    boolean xxxooo = redis.getLock("xxxooo", 10);
    System.out.println("xxxooo = " + xxxooo);

    return "ok";
  }

  //========================= flowable
  @RequestMapping("test/start")
  public void start() {
    HashMap<String, Object> vars = new HashMap<>(0);

    List<SysUser> auditors = new LinkedList<>();
    SysUser a1 = new SysUser().setSysUserId(111L).setUsername("张三");
    SysUser a2 = new SysUser().setSysUserId(222L).setUsername("李四");
    auditors.add(a1);
    auditors.add(a2);

    ProcessInfo processInfo = new ProcessInfo();
    processInfo.setTitle("武大朗的请假申请");
    processInfo.setUserId(100L);
    processInfo.setBizType(1);
    processInfo.setBizKey(3234234L + "");
    standardFlowService.startProcess(processInfo, auditors);

  }

  @RequestMapping("test/listTask")
  public Object listTask(Long userId) {
    return standardFlowService.listTask(userId);

  }

  @RequestMapping("test/submitTask")
  public void submitTask(Long userId, String taskId, UserAuditInfo userAuditInfo) {

    standardFlowService.submitTask(taskId, userAuditInfo);

  }

  // ======BloomFilter

  private static BloomFilterHelper<Integer> bloomFilterHelper;

  static {
    bloomFilterHelper = new BloomFilterHelper<>(Funnels.integerFunnel(), 10000);
  }

  @RequestMapping("test/putBf")
  public Object putBf() {

    for (int i = 0; i < 100; i++) {
      redis.putBloomFilter(bloomFilterHelper, "aa", i);
    }

    int num = new Random().nextInt(200);
    System.out.println("num = " + num);

    return redis.mightContain(bloomFilterHelper, "aa", num);
  }

  @Value("${hello:xxx}")
  private String hello;

  @RequestMapping("test/nacos")
  public void nacos() {
    System.out.println("hello = " + hello);
  }

}
